package pw.hais.braintwists.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.squareup.okhttp.Response;

import pw.hais.braintwists.R;
import pw.hais.braintwists.app.BaseActivity;
import pw.hais.braintwists.dao.LeanCloudDao;
import pw.hais.braintwists.model.User;
import pw.hais.http.base.OnHttpListener;
import pw.hais.utils.L;

/**
 * Created by Hais1992 on 2015/12/11.
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener {
    private EditText edit_username, edit_password;
    private Button btn_login;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        edit_username = (EditText) findViewById(R.id.edit_username);
        edit_password = (EditText) findViewById(R.id.edit_password);
        btn_login = (Button) findViewById(R.id.btn_login);
        btn_login.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                if (verificationInfo()) {
                    doLogin();
                }
                break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_login, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_reg) {
            startActivityForResult(new Intent(context, RegisterActivity.class), 100);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 222) {
            finish();
            L.showShort("自动登录成功！");
        }
    }


    //验证信息
    private String username;
    private String password;

    private boolean verificationInfo() {
        username = edit_username.getText() == null ? "" : edit_username.getText().toString().trim();
        password = edit_password.getText() == null ? "" : edit_password.getText().toString().trim();

        if ("".equals(username) || "".equals(password)) {
            L.showShort("用户名、密码不能为空！");
        } else {
            return true;
        }

        return false;
    }

    //开始注册
    private void doLogin() {
        loadDialogShow("登录中...");
        LeanCloudDao.loginUser(username, password, new OnHttpListener<User>() {
            @Override
            public void onSuccess(Response response, User data) {
                loadDialogDismiss();
                if (data.error != null) {
                    L.showShort("登录失败," + data.error + "~~");
                } else {
                    L.showShort(data.name + "，登录成功！");
                    app.user = data;
                    finish();
                }
            }
        });
    }
}
